package hotLeetcode;

import java.util.ArrayList;
import java.util.List;

public class H46Leetcode {
    public List<List<Integer>> permute(int[] nums) {

        List<Integer> path = new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        boolean[] used = new boolean[nums.length];
        dfs(nums, res, path, used);

        return res;

    }


    private void dfs(int[] nums, List<List<Integer>> res, List<Integer> path, boolean[] used) {
        //递归结束条件
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }

        //遍历整个数组
        for (int i = 0; i < nums.length; i++) {
            if (used[i]) continue;

            path.add(nums[i]);

            used[i] = true;

            //递归
            dfs(nums, res, path, used);

            //回溯
            path.remove(path.size() - 1);
            used[i] = false;



        }


    }


    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        H46Leetcode h46Leetcode = new H46Leetcode();
        List<List<Integer>> path = h46Leetcode.permute(nums);
        System.out.println(path);

    }
}
